package cn.xopencode.scoreservice.service.demand.impl;
import cn.hutool.core.util.IdUtil;
import cn.xopencode.common.framework.vo.PageResult;
import cn.xopencode.score.api.enums.innovate.SuggestScoreEnum;
import cn.xopencode.score.api.enums.safety.ParticipatingRoleScoreEnum;
import cn.xopencode.score.api.enums.safety.RankingScoreEnum;
import cn.xopencode.score.api.rpc.demand.dto.*;
import cn.xopencode.scoreservice.convert.demand.ScoreAllConvert;
import cn.xopencode.scoreservice.convert.demand.ScoreConvert;
import cn.xopencode.scoreservice.dal.mysql.dataobject.demand.ScoreAllDO;
import cn.xopencode.scoreservice.dal.mysql.dataobject.demand.ScoreDO;
import cn.xopencode.scoreservice.dal.mysql.mapper.demand.ScoreMapper;
import cn.xopencode.scoreservice.service.demand.ScoreService;
import cn.xopencode.securityservice.enums.suggest.SuggestLevelEnum;
import cn.xopencode.securityservice.rpc.activity.dto.ActivityCreateReqDTO;
import cn.xopencode.securityservice.rpc.suggest.dto.SuggestCreateReqDTO;
import cn.xopencode.securityservice.rpc.violate.dto.ViolateCreateReqDTO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 积分情况 Service 实现
 */
@Service
@Validated
public class ScoreServiceImpl implements ScoreService {
    @Autowired
    private ScoreMapper scoreMapper;

    /**
     * 照日期时间倒序展示用户每月的积分记录
     * @return
     */
    @Override
    public PageResult<ScoreRespDTO> pageScore(ScoreReqDTO scoreReqDTO) {
        IPage<ScoreDO> scoreDO = scoreMapper.selectPage(
                scoreReqDTO.setSorts(Collections.singletonList(ScoreReqDTO.UPDATETIME_DESC)));
        return ScoreConvert.INSTANCE.convertPage(scoreDO);
    }

    /**
     * 根据日期获取以及员工编号找到指定数据
     * @param scoreReqDTO 根据日期查询
     * @return
     */
    @Override
    public ScoreRespDTO getScoreFromDate(ScoreReqDTO scoreReqDTO) {
        ScoreDO scoreDO = scoreMapper.selectScoreFromDate(scoreReqDTO.getEmpId(),scoreReqDTO.getUpdateTime());
        System.out.println("scoreDO:"+scoreDO);
        return ScoreConvert.INSTANCE.convert(scoreDO);
    }

    /**
     * 根据id找到对应数据
     * @param id ID
     * @return
     */
    @Override  //mybatis-plus  通用mapper
    public List<ScoreAllDTO> getDetailsScore(String id) {
        List<ScoreAllDO>  lists= scoreMapper.selectAllByEmpId(id);
         System.out.println("进入getDetailsScore");
        return ScoreAllConvert.INSTANCE.convertList(lists);
    }

    /**
     * 创新与合理化建议
     * 积分规则：车间级+1分，公司级+2分。得分数据传给后台，前端不展示
     * @param createDTO
     * @return
     */
    @Override
    public Double addSuggestScore(SuggestCreateReqDTO createDTO) {
        Date suggestTime = createDTO.getSuggestTime();
        String empId = "1001";

        Integer suggestLevel = createDTO.getSuggestLevel();
        Double resultScore = null;

        ScoreDO sdo = scoreMapper.selectScoreFromDate(empId, suggestTime);

        //检查该员工本月是否有数据，如果没有则添加
        if(sdo==null){
            sdo = new ScoreDO();
            sdo.setUpdateTime(suggestTime);
          // sdo.setSuggestScore(createDTO.getScore());
            for (SuggestLevelEnum level : SuggestLevelEnum.values()) {
                if (createDTO.getSuggestLevel() == level.getLevel()) {
                    for (SuggestScoreEnum score : SuggestScoreEnum.values()) {
                        if (level.getName().equals(score.getName())) {
                            sdo.setSuggestScore(score.getScore());
                            resultScore = score.getScore();
                        }
                    }
                }
            }

            sdo.setId(IdUtil.objectId());
            sdo.setEmpId(empId);
            sdo.setCreateTime(suggestTime);
            sdo.setCreatorId(empId);
            sdo.setUpdateTime(suggestTime);
            sdo.setTrainingScore(0.0);
            sdo.setActivityScore(0.0);
            sdo.setAccidentScore(0.0);
            sdo.setScore(resultScore);
            scoreMapper.insertScore(sdo);


        }else if(sdo!=null){
            for (SuggestLevelEnum level : SuggestLevelEnum.values()) {
                if (createDTO.getSuggestLevel() == level.getLevel()) {
                    for (SuggestScoreEnum score : SuggestScoreEnum.values()) {
                        if (level.getName().equals(score.getName())) {
                            sdo.setSuggestScore(sdo.getSuggestScore()+score.getScore());
                            sdo.setScore(sdo.getScore()+score.getScore());
                            sdo.setEmpId(empId);
                            scoreMapper.updateSuggestScore(sdo);
                            resultScore = score.getScore();
                        }
                    }
                }
            }
        }

        System.out.println("创新与合理化建议resultScore:" + resultScore);
        return resultScore;
    }

    /**
     * 安全活动与技能比武情况
     * @param createDTO
     * 得分情况=安全活动参与角色+技能比武名次
     * 观摩人员+0.5分，参与人员+1分。公司第一名+5分。公司第二名+3分，公司第三名+2分，通报表扬+1分
     * @return
     */
    @Override
    public Double addActivityScore(ActivityCreateReqDTO createDTO) {
        Date activityTime = createDTO.getActivityTime();
        String empId = "1001";
        //1:参与人员,2:观摩人员
        Integer activityType = createDTO.getParticipatingRole();
        //1:公司第一名,2:公司第二名,3:公司第三名,4:通报表扬
        Integer ranking = createDTO.getRanking();
        Double resultScore = null;

        ScoreDO sdo = scoreMapper.selectScoreFromDate(empId, activityTime);
        System.out.println("addActivityScore:"+sdo);
        //检查该员工本月是否有数据，如果没有则添加
        if(sdo==null){
            sdo = new ScoreDO();
            sdo.setUpdateTime(activityTime);
            for(ParticipatingRoleScoreEnum participatingLevel : ParticipatingRoleScoreEnum.values()){
                if(participatingLevel.getParticipatingRole()==activityType){
                    for (RankingScoreEnum rankingLevel : RankingScoreEnum.values()) {
                        if (rankingLevel.getRanking()==ranking) {
                            resultScore = participatingLevel.getScore()+rankingLevel.getScore();
                            sdo.setActivityScore(resultScore);
                        }
                    }
                }
            }
            sdo.setId(IdUtil.objectId());
            sdo.setEmpId(empId);
            sdo.setCreateTime(activityTime);
            sdo.setCreatorId(empId);
            sdo.setUpdateTime(activityTime);
            sdo.setTrainingScore(0.0);
            sdo.setSuggestScore(0.0);
            sdo.setAccidentScore(0.0);
            sdo.setScore(resultScore);
            scoreMapper.insertScore(sdo);

        }else if(sdo!=null){
            for(ParticipatingRoleScoreEnum participatingLevel : ParticipatingRoleScoreEnum.values()){
                if(participatingLevel.getParticipatingRole()==activityType){
                    for (RankingScoreEnum rankingLevel : RankingScoreEnum.values()) {
                        if (rankingLevel.getRanking()==ranking) {
                            resultScore = participatingLevel.getScore()+rankingLevel.getScore();
//                        scoreService.addActivityScore("1001",resultScore);

                            sdo.setActivityScore(sdo.getActivityScore()+resultScore);
                            sdo.setScore(sdo.getScore()+resultScore);
                            sdo.setEmpId(empId);
                            scoreMapper.updateActivityScore(sdo);

                        }
                    }
                }
            }
        }
        System.out.println("安全活动与技能比武情况resultScore:" + resultScore);
        return resultScore;
    }

    /**
     *反三违及事故情况
     * 计算方法：班组级：发现人+2分，责任人-2分
     * 车间级：责任人-3分。同班组其他人-1分
     * 公司级：责任人和同班组全部积分清0
     * @param createDTO
     * @return
     */
    @Override
    public void updateViolateScore(ViolateCreateReqDTO createDTO) {
        Date findTime = createDTO.getFindTime();
        String empId = "1001";//当前登录人
        //获取责任人
        String personLiable = createDTO.getPersonLiable();
        //获取发现人
        String discoverer = createDTO.getDiscoverer();


        //事故级别，1:车间级,2:公司级
        Integer violateLevel = createDTO.getViolateLevel();
        if(discoverer!=null&&!discoverer.equals("")){
            ScoreDO discovererDO = scoreMapper.selectScoreFromDate(discoverer, findTime);
            if(discovererDO==null){
                discovererDO = new ScoreDO();
                discovererDO.setEmpId(discoverer);
                discovererDO.setCreateTime(findTime);
                discovererDO.setCreatorId(empId);//创建人为当前用户
                discovererDO.setUpdateTime(findTime);
                discovererDO.setAccidentScore(2.0);
                discovererDO.setScore(2.0);
                addScore(discovererDO);
            }
        }

        ScoreDO personLiableDO = scoreMapper.selectScoreFromDate(personLiable, findTime);
        if(violateLevel==1){
            //责任级别为车间级
            if(personLiableDO==null){
                personLiableDO = new ScoreDO();
                personLiableDO.setEmpId(personLiable);
                personLiableDO.setCreateTime(findTime);
                personLiableDO.setCreatorId(empId);//创建人为当前用户
                personLiableDO.setUpdateTime(findTime);
                personLiableDO.setAccidentScore(-3.0);
                personLiableDO.setScore(-3.0);
                addScore(personLiableDO);

            }else{
                personLiableDO.setEmpId(personLiable);
                personLiableDO.setCreateTime(findTime);
                personLiableDO.setCreatorId(empId);//创建人为当前用户
                personLiableDO.setUpdateTime(findTime);
                personLiableDO.setAccidentScore(personLiableDO.getAccidentScore()-3.0);
                personLiableDO.setScore(personLiableDO.getSuggestScore()-3.0);
                scoreMapper.updateAccidentScore(personLiableDO);

            }

        }else if(violateLevel==2){
            //责任级别为公司级

            if(personLiableDO==null){
                personLiableDO = new ScoreDO();
                personLiableDO.setEmpId(personLiable);
                personLiableDO.setCreateTime(findTime);
                personLiableDO.setCreatorId(empId);//创建人为当前用户
                personLiableDO.setUpdateTime(findTime);
                personLiableDO.setAccidentScore(0.0);
                personLiableDO.setScore(0.0);
                addScore(personLiableDO);

            }else{
                personLiableDO.setEmpId(personLiable);
                personLiableDO.setCreateTime(findTime);
                personLiableDO.setCreatorId(empId);//创建人为当前用户
                personLiableDO.setUpdateTime(findTime);
                personLiableDO.setAccidentScore(0.0);
                personLiableDO.setScore(0.0);
                scoreMapper.updateAccidentScore(personLiableDO);

            }

        }
    }
    //反三违，添加数据
    public void addScore(ScoreDO sdo){
        sdo.setId(IdUtil.objectId());
        sdo.setTrainingScore(0.0);
        sdo.setSuggestScore(0.0);
        sdo.setActivityScore(0.0);
        scoreMapper.insertScore(sdo);
    }

}
